#!/usr/bin/python
"""#########################################"""
"""       python program by 		    """
"""			date:		    """
"""#########################################"""

import SocketServer
import ConfigParser
import string

class MyHandler(SocketServer.BaseRequestHandler):
	def handle(self):
		while 1:
			dataReceived = self.request.recv(1024)
			if not dataReceived: break
			print dataReceived
			print self.client_address
			print valid_ip
			if self.client_address[0] in valid_ip:
				self.request.send('welcome')
			else:
				self.request.send('die')

def loadConfig(file, config={}):
    """
    returns a dictionary with key's of the form
    <section>.<option> and the values 
    """
    config = config.copy()
    cp = ConfigParser.ConfigParser()
    cp.read(file)
    for sec in cp.sections():
        name = string.lower(sec)
        for opt in cp.options(sec):
            config[name + "." + string.lower(opt)] = string.strip(cp.get(sec, opt))
    return config

if __name__ == '__main__':
	## INIT PG
	global valid_ip
	_ConfigDefault = {}
	ip =  loadConfig("D:/FakeRealName/list.ini", _ConfigDefault)
	valid_ip = []
	for x  in ip.keys():
		valid_ip.append(x.split('ip.')[1])
	print valid_ip
	#valid_ip = ['61.178.60.219','61.178.60.136','127.0.0.1']
	myServer = SocketServer.TCPServer(('127.0.0.1',30000), MyHandler)
	print myServer.serve_forever()
